﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Seed_email.aspx.cs" Inherits="newBlog.CenterInc.view_inc.Seed_email" %>
<%@ import Namespace="newBlog.CenterInc.Ctrltor" %>
<%@ Import Namespace="newBlog.CenterInc.tool"  %>
<% 
    int mode = Convert.ToInt32(Request.QueryString["mode"]);//1登录验证码 2注册验证码 3评论验证码 4用户中心修改密码 5用户中心修改邮箱 6前台重置密码
    string emailName; ;//接收前台传递的邮箱
    string ver = "";//无具体用途仅用于存储生成的验证码
    string content = "您好，当前您请求的{0}验证码为：{1} 有效期为10分钟";//邮件内容主体
    function function = new function();
    User_inc user_Inc = new User_inc();
    isString isString = new isString();
    Response.ContentType = "application/json";
    switch (mode) {
        case 1:
            string username = Request.Form["username"];
            string password = Request.Form["password"];
            if (isString.isEmpty(username,password)) {
                Response.Write("{\"error\":\"用户名密码不能为空\"}");
                return;
            }
            ver = function.CreateRanNum(4);
            Session.Add("login_ver",ver);
            emailName = user_Inc.SelectEmail(username,password);
            if (function.Email(emailName,"登录验证码",string.Format(content,"登录",ver))) {
                Response.Write("{\"error\":\"验证码发送失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"登录验证码发送成功\"}");
            return;
        case 2:
            ver = function.CreateRanNum(4);
            emailName = Request.Form["emailName"];
            if (emailName==null||emailName.Length==0) {
                Response.Write("{\"error\":\"请输入邮箱\"}");
                return;
            }
            if (user_Inc.SelectEmailUser(emailName)) {
                Response.Write("{\"error\":\"该邮箱以注册过了\"}");
                return;
            }
            Session.Add(emailName+"register_ver",ver);
            if (function.Email(emailName,"注册验证码",string.Format(content,"注册",ver))) {
                Response.Write("{\"error\":\"验证码发送失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"登录验证码发送成功\"}");
            return;
        case 3:
            if (Session["user_id"]==null) {
                Response.Write("{\"error\":\"请登录\"}");
                return;
            }
            ver = function.CreateRanNum(4);
            emailName = Session["user_email"].ToString();
            Session.Add("CommentVer",ver);
            if (function.Email(emailName,"评论验证码",string.Format(content,"评论",Session["CommentVer"].ToString()))) {
                Response.Write("{\"error\":\"验证码发送失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"评论验证码发送成功\"}");
            return;
        case 4:
            if (Session["user_id"]==null) {
                Response.Write("{\"error\":\"请登录\"}");
                return;
            }
            ver = function.CreateRanNum(4);
            emailName = Session["user_email"].ToString();
            Session.Add("SafeUserVer",ver);
            if (function.Email(emailName,"您正在修改密码",string.Format(content,"用户中心",Session["SafeUserVer"].ToString()))) {
                Response.Write("{\"error\":\"验证码发送失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"验证码发送成功\"}");
            return;
        case 5:
            if (Session["user_id"]==null) {
                Response.Write("{\"error\":\"请登录\"}");
                return;
            }
            ver = function.CreateRanNum(4);
            emailName = Request.QueryString["emailName"];
            if (emailName==null||emailName.Length==0) {
                Response.Write("{\"error\":\"请输入邮箱\"}");
                return;
            }
            if (!isString.IsChar(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", emailName))
            {
                Response.Write("{\"error\":\"邮箱格式不正确哦！\"}");
                return;
            }
            if (user_Inc.SelectEmailUser(emailName)) {
                Response.Write("{\"error\":\"该邮箱以注册过了\"}");
                return;
            }
            Session.Add(emailName+"EmailVer",ver);
            if (function.Email(emailName,"您正在修改邮箱",string.Format(content,"邮箱",Session[emailName+"EmailVer"].ToString()))) {
                Response.Write("{\"error\":\"验证码发送失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"验证码发送成功\"}");
            return;
        case 6:
            emailName = Request.Form["emailName"];
            string user_name = Request.Form["user_name"];
            if (isString.isEmpty(emailName,user_name)) {
                Response.Write("{\"error\":\"用户名邮箱禁止留空\"}");
                return;
            }
             if (!isString.IsChar(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", emailName))
            {
                Response.Write("{\"error\":\"邮箱格式不正确哦！\"}");
                return;
            }
            if (user_Inc.User_nameAndEmail(user_name,emailName)) { 
            Response.Write("{\"error\":\"用户名或者邮箱有误，请重新输入\"}");
                return;
            }

            ver = function.CreateRanNum(4);
            Session.Add(emailName+"RePwdlVer",ver);
            if (function.Email(emailName,"您正在修改邮箱",string.Format(content,"邮箱",Session[emailName+"RePwdlVer"].ToString()))) {
                Response.Write("{\"error\":\"验证码发送失败\"}");
                return;
            }
            Response.Write("{\"msg\":\"验证码发送成功\"}");
            return;
        default:
            Response.Write("{\"error\":\"未输入任何内容\"}");
            return;
    }
    %>
